<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['surveys:answers:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['surveys:answers:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="answersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="用户ID" align="center" prop="userId" /> -->
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="日期" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['surveys:answers:edit']"
          >查看问卷</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['surveys:answers:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看问卷 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" class="custom-dialog">
      <el-form label-width="40px">
        <el-form-item v-for="(item,ii) in viewDataList" :key="item.id">
          <label>{{ item.id }} - {{ item.questionText }}</label>
          <div v-if="item.questionType == 0">
            <el-checkbox-group v-model="aaa[ii]">
              <el-checkbox v-for="opt in item.ebQuestionsOptionsList" :label="opt.id" :key="opt.optionText">{{opt.optionText}}</el-checkbox>
            </el-checkbox-group>
          </div>
          <div v-else-if="item.questionType == 1">
            <el-checkbox-group v-model="aaa[ii]">
              <el-checkbox v-for="opt in item.ebQuestionsOptionsList" :label="opt.id" :key="opt.optionText">{{opt.optionText}}</el-checkbox>
            </el-checkbox-group>
          </div>
          <div v-else-if="item.questionType == 2">
            <el-input type="textarea"  :rows="4" :value="item.ebQuestionsAnswers.answerText"/>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAnswers, getAnswers, delAnswers, addAnswers, updateAnswers } from "@/api/surveys/answers";

export default {
  name: "Answers",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户回答表格数据
      answersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName:null,
        phonenumber:null,
      },
      viewDataList: [],
      aaa:[]
    };
  },
  created() {
    this.getList();
  },
  computed: {
  },
  methods: {
    /** 查询用户回答列表 */
    getList() {
      this.loading = true;
      listAnswers(this.queryParams).then(response => {
        this.answersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => {return {
        userId: item.userId,
        createTime : item.createTime
      }})
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看问卷详情按钮操作 */
    handleView(row) {
      const id = {
        surveyId: row.surveyId,
        userId: row.userId,
        createTime : row.createTime
      }
      getAnswers(id).then(response => {
        //this.viewDataList = response.data;
        this.open = true;
        this.$set(this, 'viewDataList', response.data);
        this.title = "问卷详情";
        for (let dd in response.data) {
          let ddd = response.data[dd];
          if(ddd && ddd.ebQuestionsAnswers && ddd.ebQuestionsAnswers.optionId){
            let splitA = ddd.ebQuestionsAnswers.optionId.split(",")
            if(splitA.length > 1){
              let tempA = []
              for(let itemi in splitA){
                tempA.push(Number(splitA[itemi]))
              }
              this.$set(this.aaa, dd, tempA)
            }else{
              this.$set(this.aaa, dd, [Number(ddd.ebQuestionsAnswers.optionId)])
            }
            
          }else{
            this.$set(this.aaa, dd, [])
          }
        }
        console.log(this.aaa)
        // this.$forceUpdate();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let ids = null;
      if(row.userId){
        ids = [{
          userId: row.userId,
          createTime : row.createTime
        }]
      }else{
        ids = this.ids
      }
      this.$modal.confirm('是否确认删除？').then(function() {
        return delAnswers(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('surveys/answers/export', {
        ...this.queryParams
      }, `answers_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>


<style scoped lang="scss">
.custom-dialog {
  ::v-deep .el-dialog__body {
    height: 600px;
    overflow-y: scroll;
  }
}

</style>